<?php
namespace ImiApp\WebSocketServer\Controller;

// use Imi\ConnectContext;
// use Imi\Controller\WebSocketController;
// use Imi\Server\Route\Annotation\WebSocket\WSRoute;
// use Imi\Server\Route\Annotation\WebSocket\WSAction;
use Imi\Server\Route\Annotation\WebSocket\WSController;
// use Imi\Server\Route\Annotation\WebSocket\WSMiddleware;

// use Imi\Server\Route\Annotation\WebSocket\WSConfig;


use Imi\Db\Db;
// use Imi\Pool\BasePool;

/**
 * 用户表
 * @WSController
 */
class Ltd 
{
    
    public $ltd_id;
    // public function __construct($uid = null) {
    //     if($this->is_ltd_pd($uid)){
    //         $this->ltd_id = $this->is_ltd_pd($uid);
    //     }else{
    //         return false;
    //     }
    // }


    /* 
    * 是否是用户判断
    */     
    public function is_ltd_pd($uid=null)
    {
        
        if($uid){
            $db = Db::query();
            $ltd = $db->table('wst_ltd')->where('uid','=',$uid)->select()->get();
            if($ltd){
                return $ltd['id'];
            }else{
                return false;
            }
        }else{
            return false;
        }    
    }


    /* 
    * 是否是用户
    */
    public function is_ltd()
    {
        if(empty($this->ltd_id)){
            return false;
        }else{
            return true;
        }
    }


    /* 
    * 是否是完成的单子
    */
    public function get_send_status($re_uid,$send_uid,$ask_id)
    {
        $ltd = $this->get_ltd($re_uid,$send_uid,$ask_id);
        if($ltd){//有商家
            $end_status = $this->get_end_status($ltd,$ask_id);
            if($end_status=='1'){
                return false;
            }else{
                return true;
            }
        }else{
            return true;
        }
    }
    

    /* 
    * 是否是完成的单子
    */
    public function get_end_status($ltd_id,$ask_id)
    {
        $db = Db::query();
        $ask_answer = $db->table('wst_ask_answer')->where('ltd_id','=',$ltd_id)->where('ask_id','=',$ask_id)->select()->get();
        if($ask_answer){
            return $ask_answer['end_status'];
        }else{
            return false;
        }
    }


    /* 
    * 是否有询价单子
    */
    public function get_ask_answer($ltd_id,$ask_id)
    {
        $db = Db::query();
        $ask_answer = $db->table('wst_ask_answer')->where('ltd_id','=',$ltd_id)->where('ask_id','=',$ask_id)->select()->get();
        if($ask_answer){
            return true;
        }else{
            return false;
        }
    }




    /* 
    * 获取商家
    */
    public function get_ltd($re_uid,$send_uid,$ask_id = null)
    {
        $db = Db::query();
        $ask_order = $db->table('wst_ask_order')->where('id','=',$ask_id)->select()->get();
        $uid = $ask_order['uid'];
        if($re_uid == $uid){
            $ltd_id = $this->is_ltd_pd($send_uid);
        }elseif($send_uid == $uid){
            $ltd_id = $this->is_ltd_pd($re_uid);
        }
        // $re_ltd = $this->is_ltd_pd($re_uid);
        // $send_ltd = $this->is_ltd_pd($send_uid);
        // if($re_ltd && $send_ltd){
        //     //都成成立
        //     if($this->get_ask_answer($re_ltd,$ask_id)){
        //         return $re_ltd;
        //     }

        //     if($this->get_ask_answer($send_ltd,$ask_id)){
        //         return $send_ltd;
        //     }

        // }else{
        //     if($re_ltd){
        //         return $re_ltd;
        //     }else{
        //         return $send_ltd;
        //     }
        // }
        return $ltd_id;
    }

    

    /* 
    * 修改用户在线状态
    */     
    public function online($uid=null,$online = '2')
    {
        if(!$uid){
            $uid = $this->uid;
        }
        $db = Db::query();
        $res = $db->table('wst_user')->where('id','=',$uid)->update(['on_line'=>$online,'logintime'=>time()]);
        return $res;
    }

    
    /* 
    * 修改用户在线状态
    */     
    public function offline($uid=null,$online = '2')
    {
        if(!$uid){
            $uid = $this->uid;
        }
        $db = Db::query();
        $res = $db->table('wst_user')->where('id','=',$uid)->update(['on_line'=>$online,'logintime'=>time()]);
        return $res;
    }

    /* 
    * 修改用户信息
    */  
    public function update_data($uid=null,$data = array()){
        if(!$uid){
            $uid = $this->uid;
        }
        $db = Db::query();
        $res = $db->table('wst_user')->where('id','=',$uid)->update($data);
        return $res;
    }


}